#!/usr/bin/env bash

CMAPI_KEY="${CMAPI_KEY:-somekey123}"
MAXSCALE="${MAXSCALE:-false}"
IFLAG='/etc/columnstore/container-initialized'
PFLAG='/etc/columnstore/container-provisioned'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

HOSTS=("$@")
if [[ "$#" -eq 0 ]]; then
    HOSTS=(127.0.0.1)
fi

EXPANDED=()
for e in "${HOSTS[@]}"; do
    EXPANDED+=("--node ${e}")
done

if [[ -f ${PFLAG} ]]; then
    echo 'Error: Container already provisioned.'
    exit 1
fi

# Wait For Primary Node To Start
if [[ ! -f ${IFLAG} ]]; then
    echo -n 'Waiting for PM1 To Be Initialized '
    TIMEOUT=120
    while [[ ! -f "${IFLAG}" ]]; do
        if [[ $TIMEOUT -eq 0 ]]; then
            echo -e " ${RED}fail${NC}" >&2
            exit 1
        fi
        printf '.'
        sleep 2
        TIMEOUT=$((TIMEOUT - 2))
    done
    echo -e " ${GREEN}done${NC}"
fi

SEC_TO_WAIT=15
echo -n "Waiting CMAPI to finish startup on PM1"
success=false
for i in  $(seq 1 $SEC_TO_WAIT); do
    echo -n "..$i"
    if ! $(mcs cmapi is-ready > /dev/null); then
        sleep 1
    else
        success=true
        break
    fi
done

echo
if $success; then
    echo -e "${GREEN}CMAPI ready to handle requests.${NC}"
else
    echo -e "${RED}CMAPI not ready after waiting $SEC_TO_WAIT seconds. Check mcs_cli.log file for further details.${NC}"
    exit 1
fi


# Set API Key
if ! mcs cluster set api-key --key "${CMAPI_KEY}" >/dev/null 2>&1; then
    echo "Failed Setting API Key"
    exit 1
fi

# Add Nodes To System
if mcs cluster node add "${EXPANDED[@]}" >/dev/null 2>&1; then
    echo -e "Adding PM(s) To Cluster ... ${GREEN}done${NC}"
else
    echo -e "Adding PM(s) To Cluster ... ${RED}fail${NC}"
    exit 1
fi

if [[ ${MAXSCALE} == true ]]; then
    provision-mxs
fi

# Restart Cluster
if mcs cluster restart >/dev/null 2>&1; then
    echo -e "Restarting Cluster ... ${GREEN}done${NC}"
else
    echo -e "Restarting Cluster ... ${RED}fail${NC}"
    exit 1
fi

# Validate
QUERY=$(mariadb -sN -e "DROP DATABASE IF EXISTS validate; CREATE DATABASE validate; CREATE TABLE validate.sample (id INT) ENGINE=columnstore; INSERT INTO validate.sample VALUES (99); SELECT id FROM validate.sample;" 2>&1)
if [[ ${QUERY} = 99 ]]; then
    echo -e "Validating ColumnStore Engine ... ${GREEN}done${NC}"
    mariadb -e "DROP DATABASE IF EXISTS validate;" 2>&1
else
    echo -e "Validating ColumnStore Engine ... ${RED}fail${NC}"
    exit 1
fi

touch "${PFLAG}"
